﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PocketBook.Domain.Entities;
using PocketBook.Domain.Abstract;
using PocketBook.Domain.Concrete;
using GalaSoft.MvvmLight.Command;
using PocketBook.UI.View;
using PocketBook.UI.ViewModel.Dialogs;
using System.Collections.ObjectModel;
using PocketBook.UI.Model;

namespace PocketBook.UI.ViewModel.Workspaces
{
    public class ExpensesWorkspaceViewModel : WorkspaceViewModel
    {
        private MainWindowViewModel _parent;
        private IModelFactory _model;

        #region Constructor

        public ExpensesWorkspaceViewModel(MainWindowViewModel parent)
        {
            base.DisplayName = "Expenses";
            _parent = parent;
            _model = ViewModelLocator.Kernel.Get<ModelFactory>();
            ExpensesList = new ObservableCollection<Expense>(_model.GetExpenses());
        }

        #endregion

        #region ExpensesList

        private const string ExpensesListPropertyName = "ExpensesList";
        private ObservableCollection<Expense> _expensesList;

        public ObservableCollection<Expense> ExpensesList
        {
            get
            {
                return _expensesList;
            }
            set
            {
                if (_expensesList == value)
                {
                    return;
                }
                _expensesList = value;
                RaisePropertyChanged(ExpensesListPropertyName);
            }
        }

        #endregion

        #region SelectedExpense

        private const string SelectedExpensePropertyName = "SelectedExpense";
        private Expense _selectedExpense;

        public Expense SelectedExpense
        {
            get
            {
                return _selectedExpense;
            }
            set
            {
                if (_selectedExpense == value)
                {
                    return;
                }
                _selectedExpense = value;
                RaisePropertyChanged(SelectedExpensePropertyName);
            }
        }

        #endregion

        #region Commands

        private RelayCommand _addExpenseCommand;
        public RelayCommand AddExpenseCommand
        {
            get
            {
                if (_addExpenseCommand == null)
                {
                    _addExpenseCommand = new RelayCommand(() => this.AddExpense());
                }
                return _addExpenseCommand;
            }
        }

        private RelayCommand _editExpenseCommand;
        public RelayCommand EditExpenseCommand
        {
            get
            {
                if (_editExpenseCommand == null)
                {
                    _editExpenseCommand = new RelayCommand(() => this.EditExpense(), () => SelectedExpense != null);
                }
                return _editExpenseCommand;
            }
        }

        private RelayCommand _deleteExpenseCommand;
        public RelayCommand DeleteExpenseCommand
        {
            get
            {
                if (_deleteExpenseCommand == null)
                {
                    _deleteExpenseCommand = new RelayCommand(() => this.DeleteExpense(), () => SelectedExpense != null);
                }
                return _deleteExpenseCommand;
            }
        }

        #endregion

        #region Private Helpers

        private void AddExpense()
        {
            var win = new PocketBookDialog();
            win.DataContext = new AddOrUpdateExpenseViewModel(win, this);
            win.Title = "Pocket Book - Add expense";
            win.ShowDialog();
        }

        private void EditExpense()
        {
            var win = new PocketBookDialog();
            win.DataContext = new AddOrUpdateExpenseViewModel(win, this, _selectedExpense);
            win.Title = "Pocket Book - Edit expense";
            win.ShowDialog();
        }

        private void DeleteExpense()
        {
            var win = new PocketBookDialog();
            win.DataContext = new ConfirmDialogViewModel(
                win,
                "Are you sure you want to delete this expense?",
                () => { _model.DeleteExpense(SelectedExpense.ExpenseId); this.ExpensesList.Remove(SelectedExpense); });
            win.Title = "Pocket Book - Confirm";
            win.ShowDialog();
        }

        #endregion
    }
}
